package com.ponhu.ea2.goodsCenter.provider.schedule;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ponhu.ea2.entity.Goods;
import com.ponhu.ea2.goodsCenter.provider.biz.PonHuGoodsService;
import com.ponhu.ea2.goodsCenter.provider.mapper.PonHuGoodsTradePriceInfoMapper;
import com.ponhu.ea2.goodsCenter.provider.service.GoodsService;
import com.ponhu.ea2.goodsCenter.provider.support.RedisSupportService;
import com.ponhu.ea2.vo.PonHuGoodsTradePriceInfoVo;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品卖点语音转换任务
 *
 * @author zhaokai
 */
@Slf4j
@Component
@RefreshScope
public class GoodsSellingAudioTask {

    @Resource
    private GoodsService goodsService;
    @Resource
    private PonHuGoodsService ponHuGoodsService;
    @Resource
    private PonHuGoodsTradePriceInfoMapper ponHuGoodsTradePriceInfoMapper;

    @XxlJob("goodsSellingAudioTask")
    public void process() {
        long startTime = System.currentTimeMillis();
        log.info("商品卖点语音转换任务-start");
        List<PonHuGoodsTradePriceInfoVo> list =
                ponHuGoodsTradePriceInfoMapper.queryPonhuGoodsToSanLiTunNoGoodsSellingAudio();
        ponHuGoodsService.processData(list);
        log.info("商品卖点语音转换任务-END，一共耗时：{}ms", System.currentTimeMillis() - startTime);
    }

    @XxlJob("goodsSellingAudioTask2")
    public void process2() {

        log.info("商品卖点文本转语音并入库任务 ===>> start");

        LambdaQueryWrapper<Goods> queryWrapper = Wrappers
                .lambdaQuery(Goods.class)
                .isNull(Goods::getSellingPointAudio)
                .last("LIMIT 1000");
        List<Goods> goodsList = goodsService.list(queryWrapper);

        int maxTimes = 100;
        while (goodsList.size() > 0 && maxTimes-- > 0) {
            goodsList.stream().forEach(goods -> {
                try {
                    String audioUrl = ponHuGoodsService.processSellingPointAudioUrl(goods.getSellingPoint());
                    goods.setSellingPointAudio(audioUrl);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error("商品卖点文本转语音异常 ===>> goodsId={}, sellingPoint={} ===>> error={}",
                            goods.getId(), goods.getSellingPoint(), e.getMessage());
                }
            });

            boolean allMatch = goodsList.parallelStream().allMatch(goods -> StringUtils.isNotBlank(goods.getSellingPointAudio()));
            if (BooleanUtils.isNotTrue(allMatch)) {
                // 设置任务结果
                XxlJobHelper.handleFail("商品卖点文本转语音异常");
                log.error("商品卖点文本转语音异常 ===>> goodsId={}, sellingPoint={} ===>> error={}");
                return;
            }

            if (!goodsService.updateBatchById(goodsList))
                log.error("商品卖点文本转语音并入库任务异常：数据库操作异常");

            goodsList = goodsService.list(queryWrapper);
        }

        // 设置任务结果
        XxlJobHelper.handleSuccess("商品卖点文本转语音并入库任务 ===>> 成功");
        log.info("商品卖点文本转语音并入库任务 ===>> end");
    }

}
